package com.lepu.app.usercenter;

import java.io.File;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.application.MyApplication;
import com.app.utils.LoginUtil;
import com.app.utils.ObserverConst;
import com.app.utils.SdLocal;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.core.BitmapManager;
import com.core.lib.ui.wheel.WheelView;
import com.core.lib.utils.CallBack;
import com.core.lib.utils.DateType;
import com.core.lib.utils.ImageCompressUtils;
import com.core.lib.utils.ObserverManager;

import com.core.lib.utils.geneal.UploadMenuImage;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.main.ImageUtilBase;
import com.core.lib.utils.main.LogUtilBase;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.widget.CircularImageView;
import com.core.lib.widget.MyDialog;
import com.eyzhs.app.R;
import com.lepu.app.fun.my.MyAddressManageActivity;
import com.lepu.app.main.permission.PermissionUtil;
import com.lepu.app.main.permission.callback.PermissionResultCallBack;
import com.lepu.app.usercenter.bean.LoginInfo;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;

public class UserInfoActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener, OnTopbarNewRightButtonListener,
		AsyncRequest {

	private static final String REQUEST_GET_INFO = "request_get_info";
	private static final String REQUEST_MODIFY_INFO = "request_modify_info";
	private static final String REQUEST_SAVE_IMAGE = "request_save_image";

	private static final int MSG_GET_INFO_SUCCESS = 10;
	private static final int MSG_GET_INFO_FAIL = 11;

	private static final int MSG_MODIFY_INFO_SUCCESS = 20;
	private static final int MSG_MODIFY_INFO_FAIL = 21;

	private static final int MSG_SAVE_IMAGE_SUCCESS = 30;
	private static final int MSG_SAVE_IMAGE_FAIL = 31;

	private static UserInfoActivity mInstance = null;

	private String mCurrentState = "";// 0 待产中 1产后 BabyBirthed
	private String mCurrentBabyGender = "";// BabyGender
	private String mCurrentShengchanType = "";// 1 顺产 2 剖腹产 BirthWay 默认1
	private String mCurrentBabyBirthDate = "";
	private String mCurrentDueDate = "";

	// private LoginInfo mLoginInfo = null;

	public static UserInfoActivity getInstance() {
		return mInstance;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		mNeedSwipeBack = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.usercenter_userinfo_activity);

		mInstance = this;
		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		mInstance = null;
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		destroy();
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("个人资料");
		topbar.setRightText("保存");
		topbar.setonTopbarNewLeftLayoutListener(this);
		topbar.setOnTopbarNewRightButtonListener(this);

		// test
		// getInfo();
		updateUserInfo();
	}

	private void destroy() {
		LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
		String oldBabyBirthed = loginInfo.BabyBirthed;
		String oldBabyGender = loginInfo.BabyGender;
		String oldBirthWay = loginInfo.BirthWay;
		String oldBabyBirthDate = loginInfo.BabyBirthDate;

		String oldDueDate = loginInfo.DueDate;

		boolean isModify = false;

		if(!TextUtils.isEmpty(mCurrentState))
		{
			if (mCurrentState.equals("1")) {
				// 产后
				if (!oldBabyBirthed.equals(mCurrentState)) {
					isModify = true;
				} else if (!oldBabyGender.equals(mCurrentBabyGender)) {
					isModify = true;
				} else if (!oldBabyBirthDate.equals(mCurrentBabyBirthDate)) {
					isModify = true;
				} else if (!oldBirthWay.equals(mCurrentShengchanType)) {
					isModify = true;
				}
			} else {
				// 待产中
				if (!oldBabyBirthed.equals(mCurrentState)) {
					isModify = true;
				} else if (!oldDueDate.equals(mCurrentDueDate)) {
					isModify = true;
				}
			}
		}

		if (!isModify) {
			finish(true);
		} else {
			String message = "退出后资料修改无效，是否继续?";
			MyDialog dialog = new MyDialog(this)
					.setMessage(message)
					.setNegativeButton(R.string.app_cancel,
							R.drawable.btn_cancel_selector,
							R.color.color_white, new View.OnClickListener() {

								@Override
								public void onClick(View v) {

								}
							})
					.setPositiveButton(R.string.app_ok,
							R.drawable.btn_ok_selector, R.color.color_white,
							new View.OnClickListener() {

								@Override
								public void onClick(View v) {
									finish(true);
								}
							});

			dialog.create(null);
			dialog.showMyDialog();
		}
	}

	public void updateUserInfo() {
		LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();

		if(loginInfo == null)
		{
			return;
		}

		CircularImageView accountIconImageView = (CircularImageView) findViewById(R.id.accountIconImageView);
		ImageView modifyIconImageView = (ImageView) findViewById(R.id.modifyIconImageView);

		accountIconImageView.setOnClickListener(mOnClickListener);
		modifyIconImageView.setOnClickListener(mOnClickListener);

		accountIconImageView.setImageResource(R.drawable.app_ic_default_avatar);
		if (!TextUtils.isEmpty(loginInfo.Avatar)) {
			String filePath = SdLocal.getUserPath(this, loginInfo.UserID);
			String imageUrl = Setting.getImageUrl(loginInfo.Avatar);

			BitmapManager.getInstance().loadBitmap(imageUrl, filePath,
					accountIconImageView, true);
		}

		TextView userNameTextView = (TextView) findViewById(R.id.userNameTextView);
		TextView currentStateValueTextView = (TextView) findViewById(R.id.currentStateValueTextView);
		LinearLayout yuchanLayout = (LinearLayout) findViewById(R.id.yuchanLayout);
		LinearLayout chanHouLayout = (LinearLayout) findViewById(R.id.chanHouLayout);

		userNameTextView.setText(loginInfo.NickName);

		mCurrentState = loginInfo.BabyBirthed;
		String[] currentStateValue = getResources().getStringArray(
				R.array.array_baby_current_state);

		if (loginInfo.BabyBirthed.equals("1")) {
			// 已出生
			yuchanLayout.setVisibility(View.GONE);
			chanHouLayout.setVisibility(View.VISIBLE);
			currentStateValueTextView.setText(currentStateValue[1]);

			TextView genderValueTextView = (TextView) findViewById(R.id.genderValueTextView);
			TextView birthdayValueTextView = (TextView) findViewById(R.id.birthdayValueTextView);
			TextView shengchanTypeValueTextView = (TextView) findViewById(R.id.shengchanTypeValueTextView);

			mCurrentBabyGender = loginInfo.BabyGender;
			if (!TextUtils.isEmpty(loginInfo.BabyGender)) {
				String[] genderArray = getResources().getStringArray(
						R.array.array_baby_gender);
				int genderIndex = Integer.parseInt(loginInfo.BabyGender);
				genderValueTextView.setText(genderArray[genderIndex - 1]);
			}

			mCurrentBabyBirthDate = loginInfo.BabyBirthDate;
			birthdayValueTextView.setText(mCurrentBabyBirthDate);

			mCurrentShengchanType = loginInfo.BirthWay;
			if (!TextUtils.isEmpty(loginInfo.BirthWay)) {
				String[] typeArray = getResources().getStringArray(
						R.array.array_mother_shengchantype);
				int typeIndex = Integer.parseInt(loginInfo.BirthWay);
				shengchanTypeValueTextView.setText(typeArray[typeIndex - 1]);
			}
		} else {
			// 未出生
			chanHouLayout.setVisibility(View.GONE);

			if (loginInfo.BabyBirthed.equals("0")) {
				yuchanLayout.setVisibility(View.VISIBLE);
				currentStateValueTextView.setText(currentStateValue[0]);

				mCurrentDueDate = loginInfo.DueDate;
				TextView yuchanValueTextView = (TextView) findViewById(R.id.yuchanValueTextView);
				yuchanValueTextView.setText(mCurrentDueDate);
			} else {
				currentStateValueTextView.setText("");
				yuchanLayout.setVisibility(View.GONE);
			}
		}

		ImageView modifyNameImageView = (ImageView) findViewById(R.id.modifyNameImageView);
		LinearLayout modifyNameLayout = (LinearLayout) findViewById(R.id.modifyNameLayout);

		if (LoginUtil.isOldUser(loginInfo.NickName)) {
			modifyNameImageView.setVisibility(View.VISIBLE);
			modifyNameLayout.setOnClickListener(mOnClickListener);
		} else {
			modifyNameImageView.setVisibility(View.GONE);
		}
	}

	private void jumpCamera()
	{
		PermissionUtil.getInstance().request(this, new String[]{Manifest.permission.CAMERA,
						Manifest.permission.READ_EXTERNAL_STORAGE,
						Manifest.permission.WRITE_EXTERNAL_STORAGE
				},
				new PermissionResultCallBack() {
					@Override
					public void onPermissionGranted() {
						//
						String temp = SdLocal.getTempPhotoPath(UserInfoActivity.this,
								"temp_camera_icon");
						UploadMenuImage.getInstance(UserInfoActivity.this).setFilePath(
								temp);
						UploadMenuImage.getInstance(UserInfoActivity.this).setIsCut(
								true);
						UploadMenuImage.getInstance(UserInfoActivity.this)
								.setIsPhotoDouble(false);

						UploadMenuImage.getInstance(UserInfoActivity.this)
								.showPopAddPhoto();
					}

					@Override
					public void onPermissionGranted(String... permissions) {
						if (permissions == null || permissions.length == 0) {
						}
					}

					@Override
					public void onPermissionDenied(String... permissions) {
						if (permissions != null) {

						}
					}

					@Override
					public void onRationalShow(String... permissions) {
						if (permissions != null) {
							UIHelper.showToast(UserInfoActivity.this, R.string.permission_tip);
							PermissionUtil.openSettingActivity(UserInfoActivity.this);
						}
					}
				});
	}

	View.OnClickListener mOnClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			int rid = v.getId();
			switch (rid) {
			case R.id.accountIconImageView:
			case R.id.modifyIconImageView:

				jumpCamera();

				break;
			case R.id.modifyNameLayout:

				Intent intent = new Intent(UserInfoActivity.this,
						UserModifyNameActivity.class);
				startActivityAnim(intent,R.anim.lib_push_down_up_in, R.anim.lib_push_down_up_out);
				break;

			default:
				break;
			}
		}
	};

	public void viewLayoutClick(View view) {

		switch (view.getId()) {
		case R.id.currentStateLayout:

			showBabyCurrentState();
			break;
		case R.id.yuchanLayout:

			showYuChanDateDialog();
			break;
		case R.id.genderLayout:

			showGender();
			break;
		case R.id.birthdayLayout:
			showBirthDateDialog();
			break;
		case R.id.shengchanTypeLayout:
			showShengChanType();
			break;
		case R.id.addressLayout:

			Intent addressIntent = new Intent(UserInfoActivity.this,
					MyAddressManageActivity.class);
			startActivity(addressIntent, true);
			break;
		case R.id.phoneAndLoginLayout:
			
			Intent loginIntent = new Intent(UserInfoActivity.this,
					UserOtherLoginActivity.class);
			startActivity(loginIntent, true);
			break;

		default:
			break;
		}
	}

	private void showBabyCurrentState() {
		final String[] array = getResources().getStringArray(
				R.array.array_baby_current_state);
		new MyDialog(UserInfoActivity.this)
				.setSimpleItems(array, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						mCurrentState = String.valueOf(which);

						LinearLayout yuchanLayout = (LinearLayout) findViewById(R.id.yuchanLayout);
						LinearLayout chanHouLayout = (LinearLayout) findViewById(R.id.chanHouLayout);
						if (which == 1) {
							yuchanLayout.setVisibility(View.GONE);
							chanHouLayout.setVisibility(View.VISIBLE);
						} else {
							yuchanLayout.setVisibility(View.VISIBLE);
							chanHouLayout.setVisibility(View.GONE);
						}

						String value = array[which];

						TextView currentStateValueTextView = (TextView) findViewById(R.id.currentStateValueTextView);
						currentStateValueTextView.setText(value);
					}
				}).create(null).show();

	}

	private void showGender() {
		final String[] array = getResources().getStringArray(
				R.array.array_baby_gender);
		new MyDialog(UserInfoActivity.this)
				.setSimpleItems(array, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						mCurrentBabyGender = String.valueOf(which + 1);

						String value = array[which];

						TextView genderValueTextView = (TextView) findViewById(R.id.genderValueTextView);
						genderValueTextView.setText(value);
					}
				}).create(null).show();

	}

	private void showShengChanType() {
		final String[] array = getResources().getStringArray(
				R.array.array_mother_shengchantype);
		new MyDialog(UserInfoActivity.this)
				.setSimpleItems(array, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						mCurrentShengchanType = String.valueOf(which + 1);

						String value = array[which];

						TextView shengchanTypeValueTextView = (TextView) findViewById(R.id.shengchanTypeValueTextView);
						shengchanTypeValueTextView.setText(value);
					}
				}).create(null).show();

	}

	private void showBirthDateDialog() {

//		LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
//		String dateStr = "";
//		if (TextUtils.isEmpty(loginInfo.BabyBirthDate)) {
//			dateStr = DateUtilBase.stringFromDate(new Date(),
//					DateUtilBase.YEAR_MONTH_DAY);
//		} else {
//			Date date = DateUtilBase.dateFromString(loginInfo.BabyBirthDate,
//					DateUtilBase.YEAR_MONTH_DAY);
//			dateStr = DateUtilBase.stringFromDate(date,
//					DateUtilBase.YEAR_MONTH_DAY);
//		}
//
//		Date date = DateUtilBase.dateFromString(dateStr,
//				DateUtilBase.YEAR_MONTH_DAY);
		MyDialog dialog = new MyDialog(UserInfoActivity.this)
				.setTitle("请选择生日")
				.setDateTimePicker(new Date(), DateType.YEAR_MONTH_DAY)
				.setNegativeButton(R.string.app_cancel,
						new View.OnClickListener() {

							@Override
							public void onClick(View arg0) {

							}
						})
				.setPositiveButton(R.string.app_ok, new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// 如果是个数,则显示为"02"的样式
						WheelView wv_year = (WheelView) v
								.findViewById(R.id.year);
						WheelView wv_month = (WheelView) v
								.findViewById(R.id.month);
						WheelView wv_day = (WheelView) v.findViewById(R.id.day);

						String parten = "00";
						DecimalFormat decimal = new DecimalFormat(parten);
						// 设置日期的显示
						String text = (wv_year.getCurrentItem() + MyDialog.START_YEAR)
								+ "-"
								+ decimal.format((wv_month.getCurrentItem() + 1))
								+ "-"
								+ decimal.format((wv_day.getCurrentItem() + 1));

						Date selectDate = DateUtilBase.dateFromString(text,
								DateUtilBase.YEAR_MONTH_DAY);
						long selectTime = selectDate.getTime();
						Date currentDate = DateUtilBase.dateFromString(
								DateUtilBase.stringFromDate(new Date(),
										DateUtilBase.YEAR_MONTH_DAY),
								DateUtilBase.YEAR_MONTH_DAY);
						long currentTime = currentDate.getTime();

						if (selectTime > currentTime) {
							UIHelper.showToast(UserInfoActivity.this,
									R.string.bbwy_sleep_tip_3);
							return;
						}

						TextView birthdayValueTextView = (TextView) findViewById(R.id.birthdayValueTextView);
						birthdayValueTextView.setText(text);

						mCurrentBabyBirthDate = text;
					}
				});

		dialog.create(null).show();
	}

	private void showYuChanDateDialog() {

//		LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
//		String dateStr = "";
//		if (TextUtils.isEmpty(loginInfo.DueDate)) {
//			dateStr = DateUtilBase.stringFromDate(new Date(),
//					DateUtilBase.YEAR_MONTH_DAY);
//		} else {
//			Date date = DateUtilBase.dateFromString(loginInfo.DueDate,
//					DateUtilBase.YEAR_MONTH_DAY);
//			dateStr = DateUtilBase.stringFromDate(date,
//					DateUtilBase.YEAR_MONTH_DAY);
//		}
//
//		Date date = DateUtilBase.dateFromString(dateStr,
//				DateUtilBase.YEAR_MONTH_DAY);
		MyDialog dialog = new MyDialog(UserInfoActivity.this)
				.setTitle("请选择预产期")
				.setDateTimePicker(new Date(), DateType.YEAR_MONTH_DAY)
				.setNegativeButton(R.string.app_cancel,
						new View.OnClickListener() {

							@Override
							public void onClick(View arg0) {

							}
						})
				.setPositiveButton(R.string.app_ok, new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// 如果是个数,则显示为"02"的样式
						WheelView wv_year = (WheelView) v
								.findViewById(R.id.year);
						WheelView wv_month = (WheelView) v
								.findViewById(R.id.month);
						WheelView wv_day = (WheelView) v.findViewById(R.id.day);

						String parten = "00";
						DecimalFormat decimal = new DecimalFormat(parten);
						// 设置日期的显示
						String date = (wv_year.getCurrentItem() + MyDialog.START_YEAR)
								+ "-"
								+ decimal.format((wv_month.getCurrentItem() + 1))
								+ "-"
								+ decimal.format((wv_day.getCurrentItem() + 1));

//						Date selectDate = DateUtilBase.dateFromString(date,
//								DateUtilBase.YEAR_MONTH_DAY);
//						long selectTime = selectDate.getTime();
//						Date currentDate = DateUtilBase.dateFromString(
//								DateUtilBase.stringFromDate(new Date(),
//										DateUtilBase.YEAR_MONTH_DAY),
//								DateUtilBase.YEAR_MONTH_DAY);
//						long currentTime = currentDate.getTime();
//
//						if (selectTime > currentTime) {
//							UIHelper.showToast(UserInfoActivity.this,
//									R.string.bbwy_sleep_tip_3);
//							return;
//						}

						TextView yuchanValueTextView = (TextView) findViewById(R.id.yuchanValueTextView);
						yuchanValueTextView.setText(date);

						mCurrentDueDate = date;
					}
				});

		dialog.create(null).show();
	}

	@SuppressWarnings("unused")
	private void getInfo() {
		LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			JSONObject params = new JSONObject();
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);

			params.put("UserID", loginInfo.UserID);
			params.put("LoginToken", loginInfo.LoginToken);

			map.put("ht", params);
		} catch (Exception e) {
			e.printStackTrace();
		}

		showProgressDialog();
		ApiClient.http_post(Setting.getUserInfoUrl(), map, null, this,
				REQUEST_GET_INFO, true);
	}

	private void saveInfo() {

		if (TextUtils.isEmpty(mCurrentState)) {
			UIHelper.showToast(this, "请选择宝宝的当前状态");
			return;
		}

		if (mCurrentState.equals("1")) {
			if (TextUtils.isEmpty(mCurrentBabyGender)) {
				UIHelper.showToast(this, "请选择宝宝的性别");
				return;
			}

			if (TextUtils.isEmpty(mCurrentBabyBirthDate)) {
				UIHelper.showToast(this, "请选择宝宝的出生日期");
				return;
			}

			if (TextUtils.isEmpty(mCurrentShengchanType)) {
				UIHelper.showToast(this, "请选择宝宝的生产方式");
				return;
			}
		} else {
			if (TextUtils.isEmpty(mCurrentDueDate)) {
				UIHelper.showToast(this, "请选择宝宝的预产期");
				return;
			}
		}

		LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			JSONObject params = new JSONObject();
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);

			params.put("UserID", loginInfo.UserID);
			params.put("LoginToken", loginInfo.LoginToken);

			if (mCurrentState.equals("1")) {
				// 已出生
				params.put("BabyBirthed", mCurrentState);

				params.put("BabyGender", mCurrentBabyGender);
				params.put("BabyBirthDate", mCurrentBabyBirthDate);

				params.put("BabyBirthWay", mCurrentShengchanType);
			} else {
				// 未出生
				params.put("BabyBirthed", mCurrentState);

				TextView yuchanValueTextView = (TextView) findViewById(R.id.yuchanValueTextView);
				params.put("DueDate", yuchanValueTextView.getText().toString());
			}

			map.put("ht", params);
		} catch (Exception e) {
			e.printStackTrace();
		}

		showProgressDialog();
		ApiClient.http_post(Setting.getModifyUserInfoUrl(), map, null, this,
				REQUEST_MODIFY_INFO, true);
	}

	@SuppressLint("HandlerLeak")
	public Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case MSG_GET_INFO_SUCCESS:
				hideProgressDialog();

				break;
			case MSG_GET_INFO_FAIL:

				hideProgressDialog();
				break;
			case MSG_MODIFY_INFO_SUCCESS:
				hideProgressDialog();

				finish(true);

				ObserverManager.getInstance().notify(
						ObserverConst.NOTIFY_MY_FRAGMENT_CHANGE, null, null);
				break;
			case MSG_MODIFY_INFO_FAIL:

				hideProgressDialog();
				break;
			case MSG_SAVE_IMAGE_SUCCESS:

				hideProgressDialog();

				String newFilePath = SdLocal.getUserPath(UserInfoActivity.this,
						LoginUtil.getUserId());

				CircularImageView accountIconImageView = (CircularImageView) findViewById(R.id.accountIconImageView);
				accountIconImageView.setImageBitmap(ImageUtilBase
						.getBitmap(newFilePath));

				ObserverManager.getInstance().notify(
						ObserverConst.NOTIFY_MY_FRAGMENT_CHANGE, null, null);

				break;
			case MSG_SAVE_IMAGE_FAIL:

				hideProgressDialog();
				break;

			default:
				break;
			}
		};
	};

	@Override
	public void onTopbarLeftLayoutSelected() {
		destroy();
	}

	@Override
	public void onTopbarRightButtonSelected() {
		saveInfo();
	}

	private void uploadHeadIcon(String filePath) {
		if (!TextUtils.isEmpty(filePath)) {
			String iconPath = SdLocal.getUserPath(this, LoginUtil.getUserId());

			try {
				ImageCompressUtils.CompressImageFile(filePath, iconPath);

				Map<String, File> fileMaps = new HashMap<String, File>();
				fileMaps.put("Avatar", new File(iconPath));

				Map<String, Object> map = new HashMap<String, Object>();
				try {
					JSONObject params = new JSONObject();
					params.put("DeviceID", Setting.DEVICEID);
					params.put("ApplicationID", Setting.APPLICATIONID);

					params.put("UserID", LoginUtil.getUserId());
					params.put("LoginToken", LoginUtil.getLoginToken());

					map.put("ht", params);
				} catch (Exception e) {
					e.printStackTrace();
				}

				showProgressDialog();
				ApiClient.http_post(Setting.getUserHeadIconUrl(), map,
						fileMaps, this, REQUEST_SAVE_IMAGE, true);

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		LogUtilBase.LogD(null, "onActivityResult");
		UploadMenuImage.getInstance(this).onActivityResult(new CallBack() {
			@Override
			public void callBackSuccess(Object obj) {
				super.callBackSuccess(obj);

				uploadHeadIcon((String) obj);
			}
		}, requestCode, resultCode, data);

	};

	@Override
	public void RequestComplete(Object object, Object data) {
		if (object.equals(REQUEST_GET_INFO)) {

			// test no use
			String result = (String) data;
			LogUtilBase.LogD(null, result);

			mHandler.sendEmptyMessage(MSG_GET_INFO_SUCCESS);

		} else if (object.equals(REQUEST_MODIFY_INFO)) {
			LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
			loginInfo.BabyBirthed = mCurrentState;
			if (mCurrentState.equals("1")) {
				loginInfo.BabyGender = String.valueOf(mCurrentBabyGender);
				loginInfo.BabyBirthDate = mCurrentBabyBirthDate;
				loginInfo.BirthWay = String.valueOf(mCurrentShengchanType);
			} else {
				loginInfo.DueDate = mCurrentDueDate;
			}
			LoginInfo.saveLoginInfo(this, loginInfo);
			mHandler.sendEmptyMessage(MSG_MODIFY_INFO_SUCCESS);
		} else if (object.equals(REQUEST_SAVE_IMAGE)) {
			String result = (String) data;
			int code = 0;
			String headIcon = "";
			try {
				JSONObject jsonObject = new JSONObject(result);
				JSONObject obj1 = (JSONObject) jsonObject.get("Result");
				code = obj1.optInt("Status");
				JSONObject obj2 = (JSONObject) obj1.get("DetailInfo");
				headIcon = obj2.optString("Avatar");
			} catch (JSONException e) {
				e.printStackTrace();
				code = 0;
			}

			if (code == 200) {

				LoginInfo loginInfo = MyApplication.getInstance()
						.getLoginInfo();
				loginInfo.Avatar = headIcon;
				LoginInfo.saveLoginInfo(this, loginInfo);

				mHandler.sendEmptyMessage(MSG_SAVE_IMAGE_SUCCESS);
			} else {
				mHandler.sendEmptyMessage(MSG_SAVE_IMAGE_FAIL);
			}
		}
	}

	@Override
	public void RequestError(Object object, int errorId, String errorMessage) {

		if (object.equals(REQUEST_GET_INFO)) {
			mHandler.sendEmptyMessage(MSG_GET_INFO_FAIL);
		} else if (object.equals(REQUEST_MODIFY_INFO)) {
			mHandler.sendEmptyMessage(MSG_MODIFY_INFO_FAIL);
		} else if (object.equals(REQUEST_SAVE_IMAGE)) {
			mHandler.sendEmptyMessage(MSG_SAVE_IMAGE_FAIL);
		}
	}
}
